local handou = fk.CreateSkill {
  name = "lvshi__handou",
  tags = { "Phase" },
}

Fk:loadTranslationTable{
  ["lvshi__handou"] = "酣斗",
  [":lvshi__handou"] = "阶段技，你可选择依次发起拼点与【决斗】，你执行第二项时获得中央区内你与对方此回合不因使用而失去的牌；你每赢一次则摸一张牌，反之弃置一张牌。",
  ["#lvshi__handou-invoke"] = "你可选择依次发起拼点与【决斗】",
  ["#lvshi__handou-duel"] = "酣斗：你可视为使用【决斗】",
  ["#lvshi__handou-pindian"] = "酣斗：你可与一名角色拼点",
  ["@@handou-inhand"] = "酣斗",
  ["pindian"] = "拼点",

  ["$lvshi__handou1"] = "驰骋疆场，快意恩仇！",
  ["$lvshi__handou2"] = "北海酬恩，神亭酣战！",
}

handou:addEffect("active", {
  anim_type = "control",
  target_num = 1,
  card_num = 0,
  max_phase_use_time = 1,
  prompt = "#lvshi__handou-invoke",
  interaction = function(self, player)
    return UI.ComboBox {
      choices = {"pindian", "duel"}
    }
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    if self.interaction.data == "pindian" then
      return player:canPindian(to_select)
    elseif self.interaction.data == "duel" then
      return player:canUseTo(Fk:cloneCard("lvshi__duel"), to_select)
    else
      return false
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    if self.interaction.data == "pindian" then
      local pindianData = player:pindian({ target }, handou.name)
      if pindianData.results[target].winner == player then
        player:drawCards(1, handou.name)
      else
        room:askToDiscard(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = handou.name,
          cancelable = false,
        })
      end
    else
      room:useVirtualCard("lvshi__duel", nil, player, target, handou.name, false)
      if player:getMark("handou-win") > 0 then
        player:drawCards(1, handou.name)
      else
        room:askToDiscard(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = handou.name,
          cancelable = false,
        })
      end
      room:setPlayerMark(player, "handou-win", 0)
    end
    local ids = {}
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
    room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if (move.from == player or move.from == target) and move.moveReason ~= fk.ReasonUse then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end, turn_event.id)
    ids = table.filter(ids, function (id)
      return table.contains(room.discard_pile, id)
    end)
    if #ids > 0 then
      room:setPlayerMark(player, "handou-cards", ids)
      room:obtainCard(player, ids, true, fk.ReasonJustMove, player, handou.name, "@@handou-inhand")
    end
    if player.dead then return end
    if self.interaction.data == "pindian" then
      room:askToUseVirtualCard(player, {
        name = "lvshi__duel",
        skill_name = handou.name,
        cancelable = false,
        prompt = "#lvshi__handou-duel",
      })
      if player:getMark("handou-win") > 0 then
        player:drawCards(1, handou.name)
      else
        room:askToDiscard(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = handou.name,
          cancelable = false,
        })
      end
      room:setPlayerMark(player, "handou-win", 0)
    else
      local to = room:askToChoosePlayers(player, {
        targets = table.filter(room:getOtherPlayers(player), function (p) return player:canPindian(p) end),
        max_num = 1,
        min_num = 1,
        skill_name = handou.name,
        cancelable = true,
        prompt = "#lvshi__handou-pindian",
      })
      if #to > 0 then
        local pindian = player:pindian(to, handou.name)
        if pindian.results[to[1]].winner == player then
          player:drawCards(1, handou.name)
        else
          room:askToDiscard(player, {
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skill_name = handou.name,
            cancelable = false,
          })
        end
      end
    end
    local to_throw = table.filter(player:getTableMark("handou-cards"), function (id)
      return table.contains(player:getCardIds("h"), id) end)
    if #to_throw > 0 then
      room:throwCard(to_throw, handou.name, player, player)
    end
  end,
})

handou:addEffect(fk.PreDamage, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(handou.name) and target == player and data.card and data.card.skillName == handou.name
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addPlayerMark(player, "handou-win")
  end,
})

return handou